package cn.zfz.controller;

import cn.zfz.entity.Account;
import cn.zfz.service.AccountService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>账目表 API接口</p>
 *
 * @author 波波老师(微信 ： javabobo0513)
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    private AccountService accountService;

    /**
     * 获取所有账目信息
     */
    @GetMapping("/getAllAccount")
    public Map getAllAccount(){
        Map<String, Object> map = new HashMap<>();
        List<Account> accountList = accountService.selectAll();
        map.put("code", "0000");
        map.put("msg", "成功");
        map.put("data", accountList);
        return map;
    }

    /**
     * 新增账目
     */
    @PostMapping("/saveAccount")
    public Map saveAccount(@RequestBody Account account){
        Map<String, Object> map = new HashMap<>();
        account.setCreateTime(new Date());
        int count = accountService.save(account);
        if(count == 1){
            map.put("code", "0000");
            map.put("msg", "成功");
        }else{
            map.put("code", "0001");
            map.put("msg", "失败");
        }
        return map;
    }

    /**
     * 更新账目
     */
    @PostMapping("/updateAccount")
    public Map updateAccount(@RequestBody Account account){
        Map<String, Object> map = new HashMap<>();
        int count = accountService.update(account);
        if(count == 1){
            map.put("code", "0000");
            map.put("msg", "成功");
        }else{
            map.put("code", "0001");
            map.put("msg", "失败");
        }
        return map;
    }

    /**
     * 删除账目
     */
    @DeleteMapping("/deleteAccount/{id}")
    public Map updateAccount(@PathVariable("id") Integer id){
        Map<String, Object> map = new HashMap<>();
        int count = accountService.deleteById(id);
        if(count == 1){
            map.put("code", "0000");
            map.put("msg", "成功");
        }else{
            map.put("code", "0001");
            map.put("msg", "失败");
        }
        return map;
    }

    /**
     * 折线图数据
     */
    @GetMapping("/getLineData")
    public Map getLineData(){
        return accountService.getLineData();
    }
}